package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.bo.QuestionBO;
import com.junzhiit.nengji.dao.QuestionDAO;
import com.junzhiit.nengji.model.Question;


@Repository
public class QuestionDAOImpl extends AbstractBaseDAO<Question> implements QuestionDAO {

	@Override
	public List<QuestionBO> findAll(int pageNumber, int maxPageRow,String questionTypeId) {
		StringBuffer sql=new StringBuffer();
		StatementParameter param=new StatementParameter();
		
		sql.append("select q.*,t.name from question q inner join question_type t on");
		sql.append(" q.question_type_id=t.id and t.is_delete=? where q.is_delete=?");
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());
		if (StringUtils.isNotBlank(questionTypeId)) {
			sql.append(" and q.question_type_Id=?");
			param.setString(questionTypeId);
		}

		sql.append(" order by q.sort desc,q.created_time asc");
		
		return queryForPagingList(sql.toString(), QuestionBO.class, param,pageNumber,maxPageRow);
	}

	@Override
	public Integer getTotalCount(String questionTypeId) {
		StringBuffer sql=new StringBuffer();
		StatementParameter param=new StatementParameter();
		
		sql.append("select count(*) from question where is_delete=?");
		param.setInt(IsDelete.NO.toIntValue());
		if (StringUtils.isNotBlank(questionTypeId)) {
			sql.append(" and question_type_Id=?");
			param.setString(questionTypeId);
		}
		
		return queryForInt(sql.toString(), param);
	}

	@Override
	public QuestionBO findById(String id) {
		StringBuffer sql=new StringBuffer();
		StatementParameter param=new StatementParameter();
		
		sql.append("select q.*,t.name from question q inner join question_type t on");
		sql.append(" q.question_type_id=t.id and t.is_delete=? where q.is_delete=? and q.id=?");
		
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(id);
		
		return query(sql.toString(), QuestionBO.class, param);
	}

	@Override
	public List<QuestionBO> findAlls() {
		StringBuffer sql=new StringBuffer();
		StatementParameter param=new StatementParameter();
		
		sql.append("select q.*,t.name from question q inner join question_type t on");
		sql.append(" q.question_type_id=t.id and t.is_delete=? where q.is_delete=?");
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());
		
		sql.append(" order by q.sort desc,q.created_time asc");
		
		return queryForList(sql.toString(), QuestionBO.class, param);
	}

	@Override
	public Integer getMaxSort() {
		StringBuffer sql = new StringBuffer();
		sql.append("select max(sort) from `question` where is_delete=?");
		
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		
		String str = queryForString(sql.toString(), params);
		if (StringUtils.isNotBlank(str)) {
			return Integer.valueOf(str);
		}
		return 0;
	}

}
